////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//   Software       :                                                         //
//   Layer          : SRC                                                     //
//   Package        :                                                         //
//   Component      :                                                         //
//                                                                            //
//   File           : myform.cpp                                              //
//   Author         : RSO ( Rafael Sotomayor Brûlé )                          //
//   EMail          : rsotomayor@savtec.cl                                    //
//   Type           : C++ Source                                              //
//   Usage          :                                                         //
//   Purpose        : Definición de ATD  globales                             //
//                                                                            //
//                                                                            //
//   who when        what                                                     //
//   ~~~ ~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//   RSO 09-10-2010  Creation                                                 //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////

#include "mylib/errlog.h"
#include "mylib/utilconf.h"
#include "myform.h"
#include "myconfig.h"

extern MyConfig   *config_g;
extern GtkBuilder *builder_g;

MyForm::MyForm() {

}

MyForm::~MyForm() {
  
}

void MyForm::show() {
  gtk_widget_show (window_a);

}

void MyForm::hide() {
  gtk_widget_hide (window_a);
}

void MyForm::setWindowParent(GtkWidget *widget_p) {
  windowParent_a = widget_p ;
}

void  MyForm::setImageButton(GtkButton *button_p,char *image_p) {
  GtkWidget *image;
  string filename;
  filename  = config_g->pathImage_a ;
  filename += image_p;  
  
  image = gtk_image_new_from_file(filename.data());
  gtk_button_set_image(button_p,  image); 
  gtk_widget_modify_bg (GTK_WIDGET(button_p), GTK_STATE_NORMAL, &mygdkcolor_a.color_mygray1);	    
  gtk_button_set_relief( button_p, GTK_RELIEF_NONE );
  
}



void MyForm::configuraBoton(char *buttonstr_p,int start_p, GtkWidget *button_p, GCallback func_p) {
  char buffername[FILENAME_MAX];  
  string filename;
  int i;
  i=start_p ;
  sprintf(buffername,"%s%c",buttonstr_p,i);
  button_p = GTK_WIDGET (gtk_builder_get_object (builder_g, buffername));  
  
  gtk_button_set_relief( GTK_BUTTON (button_p), GTK_RELIEF_NONE ); 
  g_signal_connect(GTK_OBJECT (button_p), "clicked", GTK_SIGNAL_FUNC (func_p ), GINT_TO_POINTER (i));
  sprintf(buffername,"button_%c.png",i);
  filename  = config_g->pathImage_a ;  
  filename += buffername;
  gtk_button_set_image(GTK_BUTTON(button_p), gtk_image_new_from_file(filename.data()));  
  
}

void MyForm::configuraBotonesImages(char *buttonlabel_p,char *buttonimage_p,int start_p,int end_p, GtkWidget *button_p[], GCallback func_p) {
  char buffername[FILENAME_MAX];  
  string filename;
  
  int i,contador;
  contador = 0 ;
  for ( i=start_p;i <= end_p;i++) {
    sprintf(buffername,"%s%c",buttonlabel_p,i);
    button_p[contador] = GTK_WIDGET (gtk_builder_get_object (builder_g, buffername));  

    gtk_button_set_relief( GTK_BUTTON (button_p[contador]), GTK_RELIEF_NONE ); 

    g_signal_connect(GTK_OBJECT (button_p[contador]), "clicked", GTK_SIGNAL_FUNC (func_p ), GINT_TO_POINTER (i));
    sprintf(buffername,"%s%c.png",buttonimage_p,i);
    filename  = config_g->pathImage_a ;
    filename += buffername;
    gtk_button_set_image(GTK_BUTTON(button_p[contador]), gtk_image_new_from_file(filename.data()));
    contador++ ;
  }

}

void MyForm::init() {
  srandom(time(NULL));
  gdk_color_parse ("white", &mygdkcolor_a.color_white);
  gdk_color_parse ("blue", &mygdkcolor_a.color_blue);
  gdk_color_parse ("#0050FF", &mygdkcolor_a.color_blue1);
  gdk_color_parse ("red", &mygdkcolor_a.color_red);
  gdk_color_parse ("#FF004C", &mygdkcolor_a.color_red1);

  gdk_color_parse ("green", &mygdkcolor_a.color_green);
  gdk_color_parse ("yellow", &mygdkcolor_a.color_yellow);
  gdk_color_parse ("black", &mygdkcolor_a.color_black);
  gdk_color_parse ("gray", &mygdkcolor_a.color_gray);  
  gdk_color_parse ("#4D4D4D", &mygdkcolor_a.color_mygray1);  
  gdk_color_parse ("#BFBFBF", &mygdkcolor_a.color_mygray2);  

}


void  MyForm::setBackGround(string filename_p) {
  GdkPixmap *background;
  GdkPixbuf *pixbuf;  
  GError    *error = NULL; 
  GtkStyle  *style;     
  pixbuf = gdk_pixbuf_new_from_file (filename_p.data(),&error);
  gdk_pixbuf_render_pixmap_and_mask (pixbuf, &background, NULL, 0);
  style = gtk_style_new ();
  style->bg_pixmap[0] = background;
  gtk_widget_set_style (GTK_WIDGET(window_a), GTK_STYLE(style)); 
}


gboolean MyForm::setComboBoxSelectValue(GtkComboBox *combobox_p, const gchar *value_p) {

  gchar *conf_id;
  GtkTreeModel *model;
  GtkTreePath *path;
  GtkTreeIter iter;
  gboolean found = FALSE;

  model = gtk_combo_box_get_model(combobox_p);
  path = gtk_tree_path_new_first();
  gtk_tree_model_get_iter(model, &iter, path);
  gtk_tree_path_free(path);

  do {
    gtk_tree_model_get(model, &iter, 0, &conf_id, -1);
    if ( conf_id != NULL ) {
      if (g_str_equal(conf_id, value_p))  {
        gtk_combo_box_set_active_iter(combobox_p, &iter);
        found = TRUE;
        break;
      }
    }
  } while(gtk_tree_model_iter_next (model, &iter));

  return found ;
}

